package com.ruanyin.service.impl;


import com.ruanyin.dao.UserDao;
import com.ruanyin.dao.impl.UserDaoImpl;
import com.ruanyin.domain.PageBean;
import com.ruanyin.domain.User;
import com.ruanyin.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    @Override
    public List<User> findAll() {
        // 调用dao完成查询
        return dao.findAll();
    }

    @Override
    public User findUserById(String id) {
        return dao.findUserById(Integer.parseInt(id));
    }

    @Override
    public void alterUser(User newUser) {
        dao.alterUser(newUser);
    }

    @Override
    public void deleteUserById(String id) {
        dao.deleteUserById(Integer.parseInt(id));
    }

    @Override
    public void addUser(User user) {
        dao.addUser(user);
    }

    @Override
    public PageBean<User> findUserByPage(String currentPage, String rows) {
        int cp = Integer.parseInt(currentPage);
        int rs = Integer.parseInt(rows);
        PageBean<User> pageBean = new PageBean<User>();
        pageBean.setCurrentPage(cp);
        pageBean.setRows(rs);
        pageBean.setTotalCount(dao.findTotalCount());
        int start = (cp - 1) * rs;
        List<User> list = dao.findByPage(start, rs);
        pageBean.setList(list);
        pageBean.setTotoPage((pageBean.getTotalCount() % rs == 0) ? (pageBean.getTotalCount() % rs) : (pageBean.getTotalCount() % rs + 1));
        return pageBean;
    }

}
